/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  Image,
  Platform,
  View
} from 'react-native';
import TabNavigator from 'react-native-tab-navigator';
import { StackNavigator } from 'react-navigation';
import Home from './pages/Home';
import Todo from './pages/Todo';
import SafetyCheck from './pages/SafetyCheck';
import Message from './pages/Message';
import Mine from './pages/Mine';

export default class InitApp extends Component {

  static navigationOptions = {
    title: '首页',
  };

  constructor(props) {
    super(props);
    this.state = {
      selectedTab: 'Home'  //默认选中主页
    }
  }

  render() {
    return (
      <TabNavigator>
        {/*--首页--*/}
        {this.renderTabBarItem('首页', 'icon_tabbar_homepage', 'icon_tabbar_homepage_selected','Home', '首页', Home)}
        {/*--待办事项--*/}
        {this.renderTabBarItem('待办事项', 'icon_tabbar_merchant_normal', 'icon_tabbar_merchant_selected','Todo', '待办事项', Todo)}
        {/*--安全巡查--*/}
        {this.renderTabBarItem('安全巡查', 'icon_tabbar_mine', 'icon_tabbar_mine_selected','SafetyCheck', '安全巡查', SafetyCheck)}
        {/*--消息--*/}
        {this.renderTabBarItem('消息', 'icon_tabbar_misc', 'icon_tabbar_misc_selected','Message', '消息', Message)}
        {/*--我的--*/}
        {this.renderTabBarItem('我的', 'icon_tabbar_misc', 'icon_tabbar_misc_selected','Mine', '我的', Mine)}
      </TabNavigator>
    );
  }

  renderTabBarItem(title, iconName, selectedIconName, selectedTab, componentName, component, badgeText){
    return(
      <TabNavigator.Item
        title={title}  // 传递变量,一定要加{}
        renderIcon={() => <Image source={{uri: iconName}} style={styles.iconStyle}/>} // 图标
        renderSelectedIcon={() =><Image source={{uri: selectedIconName}} style={styles.iconStyle}/>}   // 选中的图标
        onPress={()=>{this.setState({selectedTab:selectedTab})}}
        selected={this.state.selectedTab === selectedTab}
        selectedTitleStyle={styles.selectedTitleStyle}
        badgeText = {badgeText}
      >
        <NavigatorIOS
          initialRoute={{title:componentName,component:component}}
          style={{flex: 1}}
        />
      </TabNavigator.Item>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  iconStyle: {
    width: Platform.OS === 'ios' ? 30 : 25,
    height: Platform.OS === 'ios' ? 30 : 25
  }

});


